package main

import (
	"fmt"
	"sync"
)

func addNum(count *int, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 0; i < 10000; i++ {
		*count++
	}
}

// 每次运行结果都不同，小于等于30000,
// 因为有三个协程在执行，先读取count，再更新count的值，而这个过程并不具备原子性
func main() {
	var count int = 0
	var wg sync.WaitGroup
	wg.Add(3)
	go addNum(&count, &wg)
	go addNum(&count, &wg)
	go addNum(&count, &wg)

	wg.Wait()

	fmt.Println(count)

}
